<?php

class User_model extends MY_CI_Model
{

    /**
     * @access public
     */
    public function __construct()
    {
        $this->table        = 'users';
        $this->primary_id   = 'id';
        $this->primary_name = 'real_name';
        parent::__construct();
    }

    /**
     * 回收站
     * @return number
     */
    public function get_trash_count()
    {
        return parent::get_trash_count();
    }

    /**
     * 统计数量
     * @param array $filter
     * @return number
     */
    public function get_list_count($filter = array())
    {
        $this->db->from($this->table . ' as a');
        $this->list_query_filter($filter);
        return (int)$this->db->count_all_results();
    }


    /**
     * 获取分页数据
     * @param array $filter
     * @param number $page
     * @param number $page_size
     * @return multitype:Ambigous <multitype:, number> multitype:string
     */
    public function get_list($filter = array(), $page = 1, $page_size = 15)
    {

        $arr = array();
        //初始化条件
        if (isset($filter['sort']) && !empty($filter['sort'])) {
            $filter['sort'] = trim($filter['sort']);
            if (!$this->check_single_field($filter['sort'])) {
                $filter['sort'] = $this->primary_id;
            }
        } else {
            $filter['sort'] = $this->primary_id;
        }
        $filter['order'] = isset($filter['order']) && !empty($filter['order']) ? trim($filter['order']) : 'desc';
        //获取记录数
        $record_count = $this->get_list_count($filter);
        $pager        = $this->page_and_size($filter, $record_count, $page, $page_size);

        if ($pager['record_count'] > 0) {
            $this->db->select('a.*, c.role_name');
            $this->db->from($this->table . ' as a');
            $this->db->join('role as c', 'a.web_role_id = c.id ', 'left');
            $this->list_query_filter($filter);
            $this->query_list_sort_order($filter);
            $this->db->limit($pager['page_size'], $pager['start']);
            $item_list = $this->db->get()->result_array();
            $arr       = $item_list;
        }
        //echo $this->db->last_query();die;
        return array('list' => $arr, 'pager' => $pager);
    }

    /*
  * 获取列表，已in查询
  */
    public function get_filter_list($ids = [-1], $fields = '*')
    {
        $this->db->select($fields);
        $this->db->from($this->table);
        $this->db->where_in('id', $ids);
        return $this->db->get()->result_array();
    }

    /**
     * 查询条件
     * @param array $filter
     * @return void
     */
    function list_query_filter($filter = array())
    {
        if (isset($filter['id']) && $filter['id'] != '') {
            $this->db->where('a.id', $filter['id']);
        }
        if (isset($filter['active']) && $filter['active'] > 0) {
            $this->db->where('a.active', $filter['active'] - 10);
        }
        $is_delete = isset($filter['trash']) && $filter['trash'] > 0 ? 1 : 0;
        $this->db->where('a.is_delete', $is_delete);

        if (isset($filter['keywords']) && !empty($filter['keywords'])) {
            $whereKW = "(a." . $this->primary_name . " like '%" . $filter['keywords'] . "%' or a." . $this->primary_id . " like '%" . $filter['keywords'] . "%')";
            $this->db->where($whereKW);
        }
    }

    /**
     * 新增数据
     * @param unknown $datas
     */
    public function add_data($datas)
    {
        $datas['password']    = md5(strtolower(123456));
        $datas['add_time']    = date(time());

        //$datas   = $this->imp_str($datas);
        $db_data = $this->check_post_form_datas($datas);

        if (!empty($db_data)) {
            //判断名称重复
            $this->result['error'] = 0;
            if (isset($datas['mobile'])) {
                if (!$this->check_hname(0, 2, $datas['mobile'])) {
                    $this->result['error']   = 1;
                    $this->result['message'] = '手机号存在重复,请重新输入';
                }
            }
            if ($this->result['error'] == 0) {
                if ($datas['file_id'] != 0) {
                    $this->load->model("File_model");
                    $file_info               = $this->File_model->get_info($datas['file_id']);
                    $db_data['avatar']       = $file_info['image'];
                    $db_data['avatar_thumb'] = $file_info['thumb'];
                    file_txy($db_data['avatar']);
                    file_txy($db_data['avatar_thumb']);
                    //把本地图片删除
                    $file_array['image']    = $db_data['avatar'];
                    $file_array['thumb']    = $db_data['avatar_thumb'];
                    $file_array['original'] = '';
                    $this->File_model->del_file($file_array);
                }
            }
            if ($this->result['error'] == 0) {
                $this->db->insert($this->table, $db_data);
                $this->result[$this->primary_id] = $this->db->insert_id();
            }
        } else {
            $this->result['error']   = 1;
            $this->result['message'] = '未写入任何数据';
        }
        return $this->result;
    }

    /**
     * 修改数据
     * @param unknown $datas
     * @param unknown $primary_id
     * @return multitype:number string
     */
    public function edit_data($datas, $primary_id, $where_in = FALSE, $fast_edit = '')
    {
        try {
            //$datas        = $this->imp_str($datas);
            $datas['job'] = 1;
            $db_data      = $this->check_post_form_datas($datas);
            $before_info  = [];
            if ($where_in == false) {
                $before_info = $this->get_info($primary_id);
                if (($before_info['is_web_admin_role'] == 1 && $_SESSION['user_id'] != $before_info['id']) || ($before_info['is_web_admin_role'] == 1 && isset($datas['is_delete']))) {
                    $this->result['error']   = 1;
                    $this->result['message'] = '超级管理不能被修改';
                }
            }


            if (!empty($db_data)) {
                //判断重复
                $this->result['error'] = 0;
                if ($fast_edit != '') {
                    if (isset($datas['mobile'])) {
                        if (!$this->check_hname($primary_id, 2, $datas['mobile'])) {
                            $this->result['error']   = 1;
                            $this->result['message'] = '手机号存在重复,请重新输入';
                        }
                    }
                }
                if ($this->result['error'] == 0 && $where_in === 'update') {
                    //如果修改过图片
                    if ($datas['file_is_up'] == 'y') {
                        if ($datas['file_id'] != 0) {
                            $this->load->model("File_model");
                            $file_info         = $this->File_model->get_info($datas['file_id']);
                            $db_data['avatar'] = $file_info['original'];
                            file_txy($db_data['avatar']);
                            //把本地图片删除
                            $file_array['original'] = $db_data['avatar'];
                            $this->File_model->del_file($file_array);
                        } else {
                            //清除头像
                            $db_data['avatar']       = '';
                            $db_data['avatar_thumb'] = '';
                        }
                    }
                }

                if ($this->result['error'] == 0) {
                    if ($where_in) {
                        $this->db->where_in($this->primary_id, $primary_id);
                    } else {
                        $this->db->where($this->primary_id, $primary_id);
                    }
                    $this->db->update($this->table, $db_data);
                    $this->result[$this->primary_id] = $primary_id;
                }
            } else {
                $this->result['error']   = 1;
                $this->result['message'] = '未更新任何数据';
            }
        } catch (Exception $e) {
            $this->result['error']   = 1;
            $this->result['message'] = $e->getMessage();
        }
        return $this->result;
    }

    /**
     * 判断字段是否已经存在
     * @param unknown $primary_id >0 添加   <0 修改
     * @param unknown $type 1：判断工号  2：判断手机号
     * @return multitype: $val 要判断的值
     */
    public function check_hname($primary_id, $type, $val)
    {
        $this->db->select('a.' . $this->primary_id);
        $this->db->from($this->table . ' as a');
        $wh = "";
        if ($primary_id > 0) {
            $wh = ' and a.' . $this->primary_id . " <> " . $primary_id;
        }
        if ($type == 2) {
            $whereKW = "a.mobile ='" . $val . "' ";
        }
        $whereKW .= $wh;
        $this->db->where($whereKW);
        $infos = $this->db->get()->row_array();
        //print_r($this->db->last_query());
        if (empty($infos[$this->primary_id])) {
            return true; //不存在重复
        } else {
            return false; //存在重复
        }
    }

}

?>